package com.dmx.cep.action.epgManage;

import com.dmx.cep.entity.Channel;
import com.dmx.cep.entity.ChannelType;
import com.dmx.cep.entity.TvContentInfo;
import com.dmx.cep.service.EpgService;

import java.util.*;

import com.dmx.fw.web.struts2.action.CommonQueryAction;

public class ChannelAction extends CommonQueryAction{
	
	private Channel channel;
	
	private Long id;
	
	private List<ChannelType> channelTypeList;
	
	private EpgService epgService;
	
	private String q_name;
	
	private String q_code;
	
	private String channelCode;
	
	public String execute() throws Exception{
        this.busiId="searchChannel";
		
        if("addInput".equals(this.act)){
        	return doAddInput();
        }
        
        if("addSubmit".equals(this.act)){
        	return doAddSubmit();
        }        

        if("editInput".equals(this.act)){
        	return doEditInput();
        }
        
        if("editSubmit".equals(this.act)){
        	return doEditSubmit();
        } 
        
        if("delete".equals(this.act)){
        	return doDelete();
        }      
        
		return super.execute();
	}
	
	//进入增加输入界面
	public String doAddInput(){
		
		channelTypeList=epgService.getChannelTypeList();
		
		return INPUT;
	}
	
	//执行增加提交
	public String doAddSubmit() throws Exception{
		
		try{
			if(epgService.isChannelIdExists(channel.getChannelId())){
				this.setErrorInfo(this.getText("channel.id.exists"));
				return doAddInput();
			} 
			if(epgService.isChannelCodeExists(channel.getCode().trim())){
				this.setErrorInfo(this.getText("channel.code.exists"));
				return doAddInput();
			} 	
			if(epgService.isChannelNameExists(channel.getName().trim())){
				this.setErrorInfo(this.getText("channel.name.exists"));
				return doAddInput();
			} 			
			epgService.saveObject(channel);
			
		}catch(Exception e){
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
			return doAddInput();
		}
		this.setSuccessinfo(this.getText("info.addSuccess"));
		
		return "list";
	}
	
	//进入修改输入界面
	public String doEditInput(){
		if(channel==null)
			channel=(Channel)epgService.getObject(Channel.class, id);
		
		channelTypeList=epgService.getChannelTypeList();
		
		return INPUT;
	}
	
	//执行修改提交
	public String doEditSubmit() throws Exception{
		
		try{
			Channel oldChannel=(Channel)epgService.getObject(Channel.class,channel.getChannelId());
			
			/*if(!channel.getCode().trim().equals(oldChannel.getCode().trim())){
				if(epgService.isChannelCodeExists(channel.getCode().trim())){
					this.setErrorInfo(this.getText("channel.code.exists"));
					return doEditInput();
				}
			}*/
			
			if(!channel.getName().trim().equals(oldChannel.getName().trim())){
				if(epgService.isChannelNameExists(channel.getName().trim())){
					this.setErrorInfo(this.getText("channel.name.exists"));
					return doEditInput();
				} 
			}
			
			epgService.saveObject(channel);
			
		}catch(Exception e){
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
			return doEditInput();
		}
		
		this.setSuccessinfo(this.getText("info.modifySuccess"));
		return "list";
	}
	
	public String doDelete() throws Exception{
		
		try{
			List<TvContentInfo> contentList=epgService.getTvContentListByChannelCode(channelCode);
			if(contentList!=null&&contentList.size()>0){
				this.setErrorInfo(this.getText("channel.delete.no.permission"));
				
				return "list";
			}
			
			epgService.removeObject(Channel.class, id);
			
			this.setSuccessinfo(this.getText("info.deleteSuccess"));
		}catch(Exception e){
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
		}
		
		return "list";
	}

	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public List<ChannelType> getChannelTypeList() {
		return channelTypeList;
	}

	public void setChannelTypeList(List<ChannelType> channelTypeList) {
		this.channelTypeList = channelTypeList;
	}

	public EpgService getEpgService() {
		return epgService;
	}

	public void setEpgService(EpgService epgService) {
		this.epgService = epgService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getQ_name() {
		return q_name;
	}

	public void setQ_name(String q_name) {
		this.q_name = q_name;
	}

	public String getQ_code() {
		return q_code;
	}

	public void setQ_code(String q_code) {
		this.q_code = q_code;
	}

	public String getChannelCode() {
		return channelCode;
	}

	public void setChannelCode(String channelCode) {
		this.channelCode = channelCode;
	}
}
